import time

from utils.util import selectForOne


async def testOrderEditor(browser,isContinuous=False):
    try:
        if isContinuous:
            browser.find_element_by_xpath("//span[contains(text(),'新增')]").click()
            time.sleep(0.5)
        else:
            browser.find_element_by_xpath("//span[contains(text(),'销售管理')]").click()  # 点击导航
            time.sleep(0.5)
            browser.find_element_by_xpath("//span[contains(text(),'订货单据')]").click()  # 点击菜单
            # browser.find_element_by_xpath("订货单据").click()
            time.sleep(2)
            browser.find_element_by_class_name('bill-title').click()
            time.sleep(0.5)
            browser.find_element_by_xpath("//span[contains(text(),'新增')]").click()
            time.sleep(0.5)

        browser.find_element_by_xpath(
            "//span[contains(text(),'客户名称:')]/following-sibling::div/div/input").click()  # 选客户
        time.sleep(1)
        selectForOne(browser)
        time.sleep(0.1)
        # 表格需要编辑的
        tableUpdateList = [{'columnName': '存货', 'index': 0, 'isFixed': False, 'td': ''},
                           {'columnName': '件数', 'index': 0, 'isFixed': False, 'td': ''},
                           {'columnName': '备注', 'index': -1, 'isFixed': False}]
        # 固定的表头找index
        header = browser.find_elements_by_class_name('el-table__fixed-header-wrapper')
        for item in header:
            for index, item2 in enumerate(item.find_elements_by_tag_name('th')):
                for item3 in tableUpdateList:
                    if item3['columnName'] == item2.text:
                        item3['index'] = index
                        item3['isFixed'] = True
        time.sleep(0.5)
        # 正常的表头找index
        header = browser.find_elements_by_class_name('el-table__header-wrapper')
        for item in header:
            for index, item2 in enumerate(item.find_elements_by_tag_name('th')):
                for item3 in tableUpdateList:
                    if item3['columnName'] == item2.text:
                        item3['index'] = index
                        item3['isFixed'] = False

        # 固定表格找td
        table = browser.find_element_by_class_name('el-table__fixed-body-wrapper')
        tableTr = table.find_element_by_tag_name('tbody').find_element_by_tag_name('tr')
        for index, item in enumerate(tableTr.find_elements_by_tag_name('td')):
            for item2 in tableUpdateList:
                if index == item2['index'] and item2['isFixed']:
                    item2['td'] = item

        # 正常表格找td
        table = browser.find_element_by_class_name('el-table__body')
        tableTr = table.find_element_by_tag_name('tbody').find_element_by_tag_name('tr')
        for index, item in enumerate(tableTr.find_elements_by_tag_name('td')):
            for item2 in tableUpdateList:
                if index == item2['index'] and not item2['isFixed']:
                    item2['td'] = item

        # 处理表格里的逻辑
        for item in tableUpdateList:
            if item['columnName'] == '存货':
                item['td'].click()
                time.sleep(0.2)
                item['td'].click()
                time.sleep(0.8)
                selectForOne(browser, 4)
                time.sleep(1.5)
                # if browser.find_element_by_xpath("//*[@aria-label = '产品关联查询']"):
                #     print(browser.find_element_by_xpath("//*[@aria-label = '产品关联查询']").find_element_by_xpath(
                #         "//span[contains(text(),'取消')]"))
                #     browser.find_element_by_xpath("//*[@aria-label = '产品关联查询']").find_element_by_xpath(
                #         "//span[contains(text(),'取消')]").click()
                #     time.sleep(0.5)
                if browser.find_elements_by_xpath("//*[@aria-label = '产品关联查询']"):
                    browser.find_element_by_xpath("//*[@aria-label = '产品关联查询']").find_element_by_class_name(
                    'el-dialog__headerbtn').click()
                # browser.find_element_by_xpath("//*[@aria-label = '产品关联查询']").find_element_by_xpath(
                #     "//span[contains(text(),'取消')]").click()
                time.sleep(0.5)
            elif item['columnName'] == '件数':
                if item['td'].find_element_by_tag_name('input').text == '':
                    item['td'].find_element_by_tag_name('input').send_keys('1.2')

        browser.find_element_by_xpath("//span[contains(text(),'备注:')]/following-sibling::div/input").send_keys(
            "这是一条自动化测试数据！")  # 填写备注
        time.sleep(0.2)
        browser.find_element_by_xpath("//span[contains(text(),'保存')]").click()
        time.sleep(3)
    finally:
        print(browser.current_url, '完成')
        # browser.close()
